package com.gift.recommendation.modules.user.biz;

import com.gift.recommendation.Base.MyException;
import com.gift.recommendation.modules.user.dao.entity.AddressEntity;
import com.gift.recommendation.modules.user.dto.AddressReq;
import com.gift.recommendation.modules.user.dto.AddressRsp;
import com.gift.recommendation.modules.user.service.AddressService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author zzh <2658137258@qq.com>
 * @date 2023/2/16
 */
@Service
public class AddressBiz {
    @Autowired
    private AddressService addressService;

    /**
     * 添加快递地址信息
     * @param req
     */
    public void add(AddressReq req) {
        Example example = new Example(AddressEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userUuid",req.getUserUuid());
        List<AddressEntity> list = addressService.selectByExample(example);
        if (list.size() >= 15){
            throw new RuntimeException("仅允许创建15条地址信息");
        }
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setArea(getArea(req.getProvince(),req.getCity(),req.getArea(),req.getTown()));
        addressEntity.setDetail(req.getDetail());
        addressEntity.setUserUuid(req.getUserUuid());
        addressEntity.setName(req.getUserName());
        addressEntity.setUuid(UUID.randomUUID().toString());
        addressEntity.setTel(req.getTel());
        addressEntity.setIsDefault(0);
        addressService.insertSelective(addressEntity);
    }

    /**
     * 获取快递地址信息列表
     * @param req
     * @return
     */
    public List<AddressRsp> list(AddressReq req) {
        Example example = new Example(AddressEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userUuid",req.getUserUuid());
        List<AddressEntity> list = addressService.selectByExample(example);
        List<AddressRsp> result = new ArrayList<>();
        list.forEach(s ->{
            AddressRsp addressRsp = new AddressRsp();
            addressRsp.setArea(s.getArea());
            addressRsp.setDetail(s.getDetail());
            addressRsp.setName(s.getName());
            addressRsp.setTel(s.getTel());
            addressRsp.setIsDefault(s.getIsDefault());
            addressRsp.setUuid(s.getUuid());
            result.add(addressRsp);
        });
        return result;
    }

    /**
     * 根据uuid获取快递地址信息
     * @param req
     * @return
     */
    public AddressRsp getById(AddressReq req) {
        Example example = new Example(AddressEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userUuid",req.getUserUuid());
        criteria.andEqualTo("uuid",req.getUuid());
        AddressEntity addressEntity = addressService.selectByExample(example).get(0);
        AddressRsp addressRsp = new AddressRsp();
        addressRsp.setArea(addressEntity.getArea());
        addressRsp.setDetail(addressEntity.getDetail());
        addressRsp.setName(addressEntity.getName());
        addressRsp.setTel(addressEntity.getTel());
        addressRsp.setIsDefault(addressEntity.getIsDefault());
        return addressRsp;
    }

    /**
     * 根据uuid删除快递地址信息
     * @param req
     */
    public void delete(AddressReq req) {
        Example example = new Example(AddressEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userUuid",req.getUserUuid());
        criteria.andEqualTo("uuid",req.getUuid());
        addressService.deleteByExample(example);
    }

    /**
     * 根据uuid更新快递地址信息
     * @param req
     */
    public void update(AddressReq req) {
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setTel(req.getTel());
        addressEntity.setArea(getArea(req.getProvince(),req.getCity(),req.getArea(),req.getTown()));
        addressEntity.setName(req.getUserName());
        addressEntity.setDetail(req.getDetail());
        Example example = new Example(AddressEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userUuid",req.getUserUuid());
        criteria.andEqualTo("uuid",req.getUuid());
        addressService.updateByExampleSelective(addressEntity,example);
    }

    /**
     * 根据uuid设置默认收货地址
     * @param addressReq
     */
    public void isDefault(AddressReq addressReq) {
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setIsDefault(0);
        Example example = new Example(AddressEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userUuid",addressReq.getUserUuid());
        addressService.updateByExampleSelective(addressEntity,example);
        addressEntity.setIsDefault(1);
        criteria.andEqualTo("uuid",addressReq.getUuid());
        addressService.updateByExampleSelective(addressEntity,example);
    }

    /**
     * 获取默认地址
     * @param req
     * @return
     */
    public AddressRsp searchDefault(AddressReq req) {
        Example example = new Example(AddressEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userUuid",req.getUserUuid());
        criteria.andEqualTo("isDefault",1);
        List<AddressEntity> list = addressService.selectByExample(example);
        AddressRsp addressRsp = new AddressRsp();
        if (CollectionUtils.isNotEmpty(list)) {
            BeanUtils.copyProperties(list.get(0),addressRsp);
        }
        return addressRsp;
    }

    /**
     * 获取地址信息
     * @param province
     * @param city
     * @param area
     * @param town
     * @return
     */
    public String getArea(String province,String city,String area,String town){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(province);
        if (city != null && !city.equals("")){
            stringBuffer.append(" " + city);
        }
        stringBuffer.append(" " + area);
        stringBuffer.append(" " + town);
        return stringBuffer.toString();
    }


}
